package com.tyd.cams.Adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.tyd.cams.ClassManageActivity;
import com.tyd.cams.DakaFanKuiActivity;
import com.tyd.cams.GuokaocxActivity;
import com.tyd.cams.JiaoWuActivity;
import com.tyd.cams.LoginActivity;
import com.tyd.cams.R;
import com.tyd.cams.SchoolZiXunActivity;
import com.tyd.cams.StufankaActivity;
import com.tyd.cams.XuexiaojiaoshiActivity;
import com.tyd.cams.ZycjcxActivity;
import com.tyd.cams.entity.Gridp;
import com.tyd.cams.entity.SkNews;
import com.tyd.cams.entity.SkTzgg;
import com.tyd.cams.entity.SkXyjg;
import com.tyd.cams.utils.SPUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;

import okhttp3.Call;

import static android.content.ContentValues.TAG;

public class Gridadapter extends BaseAdapter {
    Context context;
    ArrayList<Gridp> gridp;

    public Gridadapter(Context context, ArrayList<Gridp> gridp) {
        this.context = context;
        this.gridp = gridp;
    }


    @Override
    public int getCount() {
        return gridp.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.bd_mc_list, viewGroup, false);

        final TextView title = view.findViewById(R.id.textView);
        ImageView image = view.findViewById(R.id.imageView5);
        title.setText(gridp.get(i).getTitle());
        image.setImageResource(gridp.get(i).getImg());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SPUtils.getUsertype().equals("1")) {
                    switch (i) {
                        case 0:
                            toZycjcx();//专业成绩查询
                            break;
                        case 1:
                            toStufanka();
//                        gettzgg();
                            break;
                        case 2:
                            toGuokaocx();
//                        getnews();
                            break;
                        case 3:
                            toXuexiaojiaoshi();
                            break;
                        case 4:
                            toClassInfo();
                            break;
                        case 5:
                            toSchoolZiXun();
                            break;
                        case 6:
                            toJiaoWu();
                            break;
                        case 7:
                            tofankui();
                            break;
                    }
                } else {
                    switch (i) {
                        case 0:
                            toZycjcx();//专业成绩查询
                            break;
                        case 1:
                            toStufanka();
//                        gettzgg();
                            break;
                        case 2:
                            toGuokaocx();
//                        getnews();
                            break;
                        case 3:
                            toXuexiaojiaoshi();
                            break;
                        case 4:
                            toSchoolZiXun();
                            break;
                        case 5:
                            toJiaoWu();
                            break;
                        case 6:
                            tofankui();
                            break;
                        default:
                            break;
                    }

                }


//                Toast.makeText(context, "正在加载 ", Toast.LENGTH_SHORT).show();
            }

            /*跳转专业成绩查询*/
            private void toZycjcx() {
                Intent intent = new Intent(context, ZycjcxActivity.class);
                context.startActivity(intent);
            }

            /*跳转反馈*/
            private void tofankui() {
                Intent intent = new Intent(context, DakaFanKuiActivity.class);
                context.startActivity(intent);
            }

            /*跳转学生饭卡*/
            private void toStufanka() {
                Intent intent = new Intent(context, StufankaActivity.class);
                context.startActivity(intent);
            }

            /*跳转国家考试成绩查询*/
            private void toGuokaocx() {
                Intent intent = new Intent(context, GuokaocxActivity.class);
                context.startActivity(intent);
            }

            /*跳转学校教室查询*/
            private void toXuexiaojiaoshi() {
                Intent intent = new Intent(context, XuexiaojiaoshiActivity.class);
                context.startActivity(intent);
            }

            private void getxyjg() {
                new Thread() {
                    public void run() {
                        OkHttpUtils.post()
                                .url("http://syist.cn/xyjg.asp")
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {
                                        Toast.makeText(context, "校园风光链接服务器超时", Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
//                                Log.i("获取的返回信息", response);
                                        Document document = Jsoup.parse(response);
                                        Elements context = document.select("#picbox > li");
//                                String imgurl = document.select("#picbox > li").select("img").attr("src");
                                        ArrayList<SkXyjg> list = new ArrayList<>();
                                        for (Element element : context) {
                                            String title = element.text();
                                            String srcurl = element.select("img").attr("src");
                                            SkXyjg skXyjg = new SkXyjg(title, srcurl);
                                            list.add(skXyjg);
                                        }
                                        Log.i("校园风光信息", list.toString());
                                        for (SkXyjg skXyjg : list) {
                                            System.out.println(skXyjg);
                                        }

                                    }
                                });
                    }
                }.start();
            }

            private void getnews() {
                new Thread() {
                    public void run() {
                        OkHttpUtils.post()
                                .url("http://www.syist.cn/kynews.asp")
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {
                                        Toast.makeText(context, "学校新闻链接服务器超时", Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
//                                Log.i("获取的返回信息", response);
                                        Document document = Jsoup.parse(response);
                                        Elements context = document.select("body > div.wrap > div.rbox > div.rboxa > ul > li");
//                                String imgurl = document.select("#picbox > li").select("img").attr("src");
                                        ArrayList<SkNews> list = new ArrayList<>();
                                        for (Element element : context) {
                                            String time = element.select("span").text();
                                            String title = element.select("a").text().substring(element.select("a").text().indexOf("【"), element.select("a").text().indexOf("】"));
                                            String con = element.select("a").text();
                                            String href = element.select("a").attr("href");
                                            SkNews skNews = new SkNews(time, title, con, "http://www.syist.cn/" + href);
                                            list.add(skNews);
                                        }
                                        Log.i("学校新闻", list.toString());
                                        for (SkNews skNews : list) {
                                            System.out.println(skNews);
                                        }

                                    }
                                });
                    }
                }.start();
            }

          /*  private void re(String wjm, String bt) {

//                Intent intent = new Intent();
                Intent intent = new Intent(context, LoginActivity.class);
                intent.putExtra("web", wjm);
                intent.putExtra("bt", bt);
//                intent.putExtra("bt",bt);
                intent.setAction("android.intent.action.ST");
                context.startActivity(intent);
//               context. startActivity(intent);
//            }错误点***
            }
            private void re_xm(String Url) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.XM");
                intent.putExtra("xm",Url);
                context.startActivity(intent);

            }*/
        });
        return view;

    }

    private void toJiaoWu() {
        Intent intent = new Intent(context, JiaoWuActivity.class);
        context.startActivity(intent);
    }

    private void toSchoolZiXun() {

        Intent intent = new Intent(context, SchoolZiXunActivity.class);
        context.startActivity(intent);
    }

    private void toClassInfo() {
        Intent intent = new Intent(context, ClassManageActivity.class);
        context.startActivity(intent);
    }


}
